package com.sihan.swvp.common;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import com.sihan.swvp.entity.sys.User;


public class SessionContext {
	private static SessionContext instance;
	private List<HttpSession> sessions;

	private SessionContext() {
		sessions = new ArrayList<HttpSession>();
	}

	public static SessionContext getInstance() {
		if (instance == null) {
			instance = new SessionContext();
		}
		return instance;
	}
	
	public int size(){
		return getInstance().sessions.size();
	}
	
	public synchronized void removeUsers(List<Long> users){
		for (HttpSession session : sessions) {
			Object ou = session.getAttribute("u");
			if(null != ou){
				User u = (User) ou;
				if(users.contains(u.getId())){
					session.removeAttribute("u");
				}
			}
		}
	}

	public synchronized void add(HttpSession session) {
		if (session != null) {
			session.setMaxInactiveInterval(1800);
			sessions.add(session);
		}
	}

	public synchronized void remove(HttpSession session) {
		if (session != null) {
			sessions.remove(session);
		}
	}
}